package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.api.domain.SkuLockVo;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.mapper.SkuStockMapper;
import com.spzx.product.service.ISkuStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.SkuStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品sku库存表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-12-20
 */
@Service
public class SkuStockServiceImpl extends ServiceImpl<SkuStockMapper, SkuStock> implements ISkuStockService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SkuStockVo getSkuStock(Long skuId) {
        // 根据skuId查询商品库存
        LambdaQueryWrapper<SkuStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(SkuStock::getSkuId, skuId)
                .select(SkuStock::getAvailableNum, SkuStock::getSaleNum);
        SkuStock skuStock = baseMapper.selectOne(queryWrapper);

        // 封装数据
        SkuStockVo skuStockVo = new SkuStockVo();
        skuStockVo.setAvailableNum(skuStock.getAvailableNum());
        skuStockVo.setSaleNum(skuStock.getSaleNum());
        skuStockVo.setSkuId(skuId);
        return skuStockVo;
    }

    @Override
    public String checkAndLock(String orderNo, List<SkuLockVo> skuLockVoList) {

        skuLockVoList.forEach(skuLockVo -> {
            Long skuId = skuLockVo.getSkuId();
            Integer num = skuLockVo.getSkuNum();

            //检查库存
            SkuStock skuStock = baseMapper.check(skuId, num);
            if (skuStock == null) {
                skuLockVo.setHaveStock(false);//库存不足
            } else {
                skuLockVo.setHaveStock(true);//库存充足
            }
        });

        if (skuLockVoList.stream().anyMatch(skuLockVo -> !skuLockVo.isHaveStock())) {

            List<SkuLockVo> stockNotEnoughList = skuLockVoList
                    .stream()
                    .filter(skuLockVo -> !skuLockVo.isHaveStock())
                    .collect(Collectors.toList());

            //组装错误信息，提示某某商品库存不足
            StringBuffer errorMsg = new StringBuffer();
            stockNotEnoughList.forEach(skuLockVo -> {
                errorMsg.append(skuLockVo.getSkuName()).append("\n");
            });
            return errorMsg.append("库存不足").toString();
        } else {
            skuLockVoList.forEach(skuLockVo -> {
                Long skuId = skuLockVo.getSkuId();
                Integer num = skuLockVo.getSkuNum();
                baseMapper.lock(skuId, num);
            });
        }

        // 如果所有商品都锁定成功的情况下，需要缓存锁定信息到redis。以方便将来解锁库存 或者 减库存
        String dataKey = "sku:lock:data:" + orderNo;
        this.redisTemplate.opsForValue().set(dataKey, skuLockVoList);

        return "";
    }


}